<?php
namespace Model;
require_once __DIR__."/../Lib/mydb.php";
use Lib\mydb;
use Model;
use Model\Request;
class Request {
    //put your code here
    public $requestID;
    public $sponsor;//发起者ID
    public $date;   //发起时间
    public $title;
    public $place;
    private static $db=null;
    function __construct($data) {
        $this->sponsor=$data["id"];
        $this->title=$data["title"];
        $this->date=$data["date"];
        $this->place=$data["place"];
         do{
         $roomid= rand(0, 999999);
        }while(self::isonly($roomid));
        $this->requestID=$roomid;
    }
     public function add(){
        if(self::$db==null)
            self::$db =new mydb();
        $sql="insert into request(no,`sponsor`,title,date,place) values(".$this->requestID.",'".$this->sponsor."','".$this->title."','".$this->date."','".$this->place."');";
        if(self::$db->exec($sql))
            return 1;
        else
            return 0;
    }
    
    public static function get($from,$to){
         if(self::$db==null)
            self::$db =new mydb();
        $sql="select * from request order by date desc limit ".$from.",".$to."  ;";
       $res=self::$db->qurey($sql);
        $arr=array();
       while ($dd=$res->fetch_assoc()){
           $arr[]=$dd;
      }
      return $arr;
    }
     public static  function clear($time) {
       if(self::$db==null)
            self::$db =new mydb();
        $sql="delete  from request where date < '".$time ."';";
        return self::$db->exec($sql);
       
    }

        public static function isonly($data) {
       if(self::$db==null)
            self::$db =new mydb();
        $sql="select * from request where sponsor=".$data.";";
        $res=self::$db->qurey($sql);
        return $res->num_rows;
    }
   
}